﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YiYiTong.Rocket.Helper
{
    public class FileHelper
    {
        /// <summary>
        /// 图片类型
        /// </summary>
        private static Dictionary<string, string> imageContentTypes = null;
        public static Dictionary<string, string> ImageContentTypes
        {
            get
            {
                if (imageContentTypes != null)
                    return imageContentTypes;
                imageContentTypes = new Dictionary<string, string>();
                imageContentTypes.Add("image/jpeg", ".jpg");
                imageContentTypes.Add("image/jpg", ".jpg");
                imageContentTypes.Add("image/tiff", ".tif");
                imageContentTypes.Add("image/gif", ".gif");
                imageContentTypes.Add("image/x-icon", ".ico");
                imageContentTypes.Add("image/png", ".png");
                return imageContentTypes;
            }
        }
        public static bool CheckIsImg(string extens)
        {
            if (string.IsNullOrWhiteSpace(extens))
                return false;
            extens = extens.ToLower();
            if (extens == ".jpg" || extens == ".jpeg" || extens == ".png" || extens == ".gif")
            {
                return true;
            }
            return false;
        }

        public static bool CheckIsExcel(string extens)
        {
            if (string.IsNullOrWhiteSpace(extens))
                return false;
            extens = extens.ToLower();
            if(extens == ".xls" || extens == ".xlsx" || extens == ".csv")
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 获取后缀名
        /// </summary>
        /// <param name="fileContextType"></param>
        /// <returns></returns>
        public static string GetFileSuffix(string fileContextType)
        {
            string result = "";
            ImageContentTypes.TryGetValue(fileContextType.ToLower(), out result);
            return result;
        }
        /// <summary>
        /// 判断是否图片类型
        /// </summary>
        /// <param name="contenttype"></param>
        /// <returns></returns>
        public static bool CheckImgContentType(string contenttype)
        {
            return !string.IsNullOrWhiteSpace(GetFileSuffix(contenttype));
        }

        private static object LockKey = new object();
        public static bool RealWriteFile(string filePath, string strs)
        {
            bool succ_write = false;
            try
            {
                lock(LockKey)
                {
                    StreamWriter sw = new StreamWriter(filePath, true);
                    try
                    {
                        sw.Write(strs);
                        succ_write = true;
                    }
                    catch (Exception eex)
                    {
                       throw new Exception("文件" + filePath + "处理发生异常：" + eex.Message);
                    }
                    finally
                    {
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("文件" + filePath + "处理发生异常：" + ex.Message);
            }
            return succ_write;
        }
    }
}
